package cn.doitedu.api;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class _35_OperatorChain_Demo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(5000);
        env.setParallelism(4);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");

        KafkaSource<String> source = KafkaSource
                .<String>builder()
                .setBootstrapServers("doitedu:9092")
                .setTopics("doit46")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setGroupId("group-1")
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();


        env.fromSource(source, WatermarkStrategy.noWatermarks(), "kfk").slotSharingGroup("default")
                .map(String::toUpperCase).slotSharingGroup("default")
                .flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String value, Collector<String> out) throws Exception {
                        String[] split = value.split(",");
                        for (String s : split) {
                            out.collect(s);
                        }
                    }
                }).slotSharingGroup("share-group-1")
                .map(s->s).slotSharingGroup("share-group-2")
                .keyBy(s -> s)
                .process(new KeyedProcessFunction<String, String, String>() {
                    @Override
                    public void processElement(String value, KeyedProcessFunction<String, String, String>.Context ctx, Collector<String> out) throws Exception {
                        out.collect(value);
                    }
                }).slotSharingGroup("share_group-1")
                .print().slotSharingGroup("share-group-1");


        env.execute();
    }

}
